package com.example.translateappjava.Adapter;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import java.util.List;

/**
 * RecyclerView adapter for displaying search history items.
 * Handles the display of search history strings and click events on them.
 */
public class SearchHistoryAdapter extends RecyclerView.Adapter<SearchHistoryAdapter.ViewHolder> {

    /**
     * Interface definition for click callbacks on search history items.
     */
    public interface OnSearchHistoryClickListener {
        /**
         * Called when a search history item is clicked.
         * @param historyItem The clicked search history text
         */
        void onSearchHistoryClick(String historyItem);
    }

    // List of search history strings to display
    private final List<String> historyItems;
    // Listener for handling click events on history items
    private final OnSearchHistoryClickListener listener;

    /**
     * Constructs a new SearchHistoryAdapter.
     * @param listener The click listener for history items
     * @param historyItems The list of search history strings to display
     */
    public SearchHistoryAdapter(OnSearchHistoryClickListener listener, List<String> historyItems) {
        this.listener = listener;
        this.historyItems = historyItems;
    }

    /**
     * Creates new ViewHolder instances when needed.
     * @param parent The ViewGroup into which the new View will be added
     * @param viewType The type of the new View
     * @return A new ViewHolder that holds a View of the given view type
     */
    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext())
                .inflate(android.R.layout.simple_list_item_1, parent, false);
        return new ViewHolder(view);
    }

    /**
     * Binds data to the ViewHolder at the specified position.
     * @param holder The ViewHolder to update
     * @param position The position of the item in the data set
     */
    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        String historyItem = historyItems.get(position);
        holder.textView.setText(historyItem);

        // Set click listener for the history item
        holder.itemView.setOnClickListener(v -> {
            if (listener != null) {
                listener.onSearchHistoryClick(historyItem);
            }
        });
    }

    /**
     * Returns the total number of items in the data set.
     * @return The total number of history items
     */
    @Override
    public int getItemCount() {
        return historyItems.size();
    }

    /**
     * ViewHolder class that holds references to the views for each history item.
     */
    static class ViewHolder extends RecyclerView.ViewHolder {
        TextView textView;

        /**
         * Initializes the ViewHolder.
         * @param itemView The root view of the history item layout
         */
        ViewHolder(View itemView) {
            super(itemView);
            textView = itemView.findViewById(android.R.id.text1);
        }
    }
}